package com.finalcial.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.finalcial.po.DeptUserType;
import com.finalcial.po.UserTypeCostTypeSalary;
import com.finalcial.po.UserTypeCosts;
import com.finalcial.service.DeptLevelService;

@Controller
@RequestMapping("/DeptLevel")
public class DeptLevelController {

	@Resource
	private DeptLevelService deptLevelService;

	@RequestMapping("getDepartmentName")
	public ModelAndView getDepartmentName(){
		//查询所有部门名称
		List<Map<String, Object>> departmentNameList = deptLevelService.getDepartmentName();
		//查询所有职称名称
		List<Map<String, Object>> userTypeNameList = deptLevelService.getUserTypeName();
		//查询所有费用类型名称
		List<Map<String, Object>> costTypeNameList = deptLevelService.getCostTypeName();
 		System.out.println(departmentNameList);
 		System.out.println(userTypeNameList);
 		System.out.println(costTypeNameList);
 		ModelAndView mav = new ModelAndView();
 		mav.addObject("departmentNameList",departmentNameList);
 		mav.addObject("userTypeNameList",userTypeNameList);
 		mav.addObject("costTypeNameList",costTypeNameList);
 		mav.setViewName("systemsetting/deptlevelcosts");
		return mav;	
	}
	
	/*根据部门ID得到职级名称*/
	@RequestMapping(value="/getDeptUserTypeName",method =RequestMethod.POST)
	public @ResponseBody List<Map<String, Object>> getDeptUserTypeName(@RequestBody Integer departmentId){
		List<Map<String, Object>> deptUserTypeNameList = deptLevelService.getDeptUserTypeName(departmentId);
		System.out.println(deptUserTypeNameList);
		return deptUserTypeNameList;	
	}
	
	/*根据（已知部门）职位ID查找费用类型的金额*/
	@RequestMapping(value="/getSalary",method =RequestMethod.POST)
	public @ResponseBody List<Map<String, Object>> getSalary(@RequestBody UserTypeCosts userTypeCosts){
		Integer userTypeId = userTypeCosts.getUserTypeId();
		List<Map<String, Object>> salaryList = deptLevelService.getSalary(userTypeId);
		System.out.println(salaryList);
		return salaryList;	
	}
	
	/*新增部门名称*/
	@RequestMapping(value="/insertDepartmentName",method =RequestMethod.POST)
	public @ResponseBody Map<String, Object> insertDepartmentName(@RequestBody String departmentName){
		Map<String, Object> map = new HashMap<String, Object>();
		//增加部门名称
		Integer num = deptLevelService.insertDepartmentName(departmentName);
		//查询所有部门名称
		List<Map<String, Object>> deptmentNameList = deptLevelService.getDepartmentName();
		System.out.println(num);
		System.out.println(deptmentNameList);
		map.put("num", num);
		map.put("deptmentNameList", deptmentNameList);
		return map;	
	}
	
	/*给部门增加职级名称*/
	@RequestMapping(value="/insertDeptUserTypeName",method =RequestMethod.POST)
	public @ResponseBody Map<String, Object> insertDeptUserTypeName(@RequestBody DeptUserType deptUserType){
		Map<String, Object> map = new HashMap<String, Object>();
		Integer departmentId = deptUserType.getDepartmentId();
		String userTypeName = deptUserType.getUserTypeName();
		Integer deptUserTypeId = null;
		//增加职级名称
		Integer num = deptLevelService.insertDeptUserTypeName(departmentId, userTypeName);
		//查询职级id
		if(num == 1){
			deptUserTypeId = deptLevelService.getDeptUserTypeId(departmentId, userTypeName);
		}
		System.out.println(num);
		System.out.println(deptUserTypeId);
		System.out.println(deptUserTypeId);
		map.put("num", num);
		map.put("deptUserTypeId", deptUserTypeId);
		map.put("userTypeName", userTypeName);
		return map;	
	}
	
	/*根据职称ID和费用类型ID新增费用类型金额*/
	@RequestMapping(value="/insertDeptCostSalary",method =RequestMethod.POST)
	public @ResponseBody Map<String, Object> insertDeptCostSalary(@RequestBody UserTypeCostTypeSalary userTypeCostTypeSalary){
		Map<String, Object> map = new HashMap<String, Object>();
		Integer userTypeId = userTypeCostTypeSalary.getUserTypeId();
		Integer costTypeId = userTypeCostTypeSalary.getCostTypeId();
		Integer salary = userTypeCostTypeSalary.getSalary();
		//添加金额
		Integer num = deptLevelService.insertDeptCostSalary(userTypeId, costTypeId, salary);
		System.out.println(num);
		System.out.println(salary);
		map.put("num", num);
		map.put("salary", salary);
		return map;	
	}
	
	/*根据职称ID和费用类型ID修改费用类型金额*/
	@RequestMapping(value="/updateDeptCostSalary",method =RequestMethod.POST)
	public @ResponseBody Integer updateDeptCostSalary(@RequestBody UserTypeCostTypeSalary userTypeCostTypeSalary){
		Integer userTypeId = userTypeCostTypeSalary.getUserTypeId();
		Integer costTypeId = userTypeCostTypeSalary.getCostTypeId();
		Integer salary = userTypeCostTypeSalary.getSalary();
		//修改金额
		Integer num = deptLevelService.updateDeptCostSalary(userTypeId, costTypeId, salary);
		System.out.println(num);
		return num;	
	}
}
